package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.dao.TbWxOrderMapper;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbWxOrder;
import com.blacksheep.mtm.service.WxOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    private TbWxOrderMapper wxOrderMapper;
    @Override
    public DataTablesResult getList(int draw, int start, int length, int[] state, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();

        PageHelper.startPage(start/length+1,length);
        List<TbWxOrder> list = wxOrderMapper.selectList("%"+search+"%",state,minDate,maxDate,orderCol,orderDir);
        PageInfo<TbWxOrder> pageInfo=new PageInfo<>(list);
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);

        return result;
    }

    @Override
    public int updateState(long id, int state) {
        int result  = wxOrderMapper.updateStateByPrimaryKey(id,state);
        if (result<1){
            throw  new XmallException("更新失败");
        }
        return 0;
    }
}
